<template>
  <Container class="member-detail">
    <div class="container-header title title-center" slot="header">
      {{id ? '编辑' : '新增'}}例外有效进货例外政策
      <Button size="large" class="fl btn-back" type="ghost" icon="ios-arrow-back" @click="jumpPrevPage">返回</Button>
    </div>
    <div class="bg-light mt pt">
      <Form :model="form" :label-width="150" ref="form" :rules="rules">
        <FormItem label="门店类型" class="wp40">
          <Select v-model="form.storeTypeId" clearable filterable placeholder='全部'>
            <Option
              v-for="item in allStoreTypeId"
              :value="item.id"
              :key="item.id">{{item.name}}</Option>
          </Select>
        </FormItem>
        <FormItem label="品牌" class="wp40">
          <BrandSelect v-model="form.brandId" placeholder='全部'></BrandSelect>
        </FormItem>
        <FormItem label="胎面宽" class="wp40">
          <Select v-model="form.tiresWidth" clearable filterable placeholder='全部'> 
            <Option v-for='item in tire_widthArr' :value='item.value' :key="item.value">{{item.text}}</Option>
          </Select>
        </FormItem>
        <FormItem label="扁平率" class="wp40">
          <Select v-model="form.tiresAspectratio" clearable filterable placeholder='全部'> 
            <Option v-for='item in tire_ratioArr' :value='item.value' :key="item.value">{{item.text}}</Option>
          </Select>
        </FormItem>
        <FormItem label="尺寸" class="wp40">
          <Select v-model="form.tiresRim" clearable filterable placeholder='全部'> 
            <Option v-for='item in tire_sizeArr' :value='item.value' :key="item.value">{{item.text}}</Option>
          </Select>
        </FormItem>
        <FormItem label="花纹" class="wp40">
          <Select v-model="form.figure" clearable filterable placeholder='全部'> 
            <Option v-for='item in tire_figure' :value='item.dataValue' :key="item.id">{{item.dataValue}}</Option>
          </Select>
        </FormItem>
        <FormItem label="开始日期" class="wp40" prop="beginDate">
          <Date-picker type="date" placeholder="开始日期" v-model="form.beginDate"></Date-picker>
        </FormItem>
        <FormItem label="结束日期" class="wp40" prop="endDate">
          <Date-picker type="date" placeholder="结束日期" v-model="form.endDate"></Date-picker>
        </FormItem>
        <FormItem label="是否计算在任务量中" class="wp40">
          <RadioGroup v-model="form.isCounting">
            <Radio :label="false">
              <span>否</span>
            </Radio>
            <Radio :label="true">
              <span>是</span>
              <InputNumber :max="10" :min="1" v-model="form.quantity" v-if="form.isCounting"></InputNumber>
            </Radio>
          </RadioGroup>
        </FormItem>
         <!-- <FormItem label="最大任务量" class="wp40" >
            <InputNumber   style="width:310px ;margin-bottom:20px" :max="1000" :min="1" v-model="form.maxCountingQuantity"></InputNumber>
        </FormItem> -->
      </Form>
    </div>
    <Loader :status="status"></Loader>
    <div class="container-footer bg-white" slot="footer">
      <Button size="large" class="fr btn-back" type="primary" @click="onSave" :disabled="disabled">确定</Button>
    </div>
  </Container>    
</template>

<script>
import BrandSelect from '@/components/BrandSelect'
import { formatDate, getTireArr, comparativeDate } from '@/util'
export default {
  components: {
    BrandSelect
  },
  data() {
    return {
      id: this.$route.query.id || '',
      allStoreTypeId: [], //所有门店类型
      tire_widthArr: [], //胎面宽
      tire_ratioArr: [], //扁平率
      tire_sizeArr: [], //尺寸
      tire_figure: [], //花纹
      status: 'pass',
      title: '新增例外有效进货商品维护政策',
      disabled: false,
      rules: {
        beginDate: [{ required: true, type: 'date', message: '请输入开始日期', trigger: 'change' }],
        endDate: [{ required: true, type: 'date', message: '请输入结束日期', trigger: 'change' }]
      },
      form: {
        storeTypeId: null,
        brandId: null,
        tiresWidth: null,
        tiresRim: null,
        tiresAspectratio: null,
        figure: null,
        isCounting: false,
        beginDate: null,
        endDate: null,
        quantity: null,
        maxCountingQuantity: ''
      }
    }
  },
  mounted() {
    this.getType()
    if (this.id) this.getInfo()
    this.getTireData()
    this.getTireFigure()
  },
  methods: {
    getType() {
      this.$ajaxGet('/storeType/findAll').then(data => {
        this.allStoreTypeId = (data || []).map(item => {
          return {
            id: item.id,
            name: item.name
          }
        })
      })
    },
    getTireData() {
      getTireArr().then(data => {
        this.tire_ratioArr = data.data.aspect_ratio.filter(item => item.val).map(item => { //扁平率
          return {
            value: item.val,
            text: item.val ? item.name : ''
          }
        })
        this.tire_sizeArr = data.data.tire_rim.filter(item => item.val).map(item => { //尺寸
          return {
            value: item.val,
            text: item.name
          }
        })
        this.tire_widthArr = data.data.width.filter(item => item.val).map(item => { //胎面宽
          return {
            value: item.val,
            text: item.name
          }
        })
      })
    },
    getTireFigure() {
      this.$ajaxGet('/product/common/list/TIRE_FIGURE').then(data =>{
        this.tire_figure = (data || []).map(item =>{
          return {
            dataValue: item.dataValue,
            id: item.id
          }
        })
      })
    },
    getInfo() {
      this.$ajaxGet('/setting/purchase_special_policy/detail/' + this.id).then(data => {
        this.form = {
          storeTypeId: data.storeTypeId,
          brandId: data.brandId,
          tiresWidth: data.tiresWidth,
          tiresRim: data.tiresRim,
          tiresAspectratio: data.tiresAspectratio,
          figure: data.figure,
          isCounting: data.isCounting,
          beginDate: data.beginDate,
          endDate: data.endDate,
          quantity: data.quantity,
          maxCountingQuantity: data.maxCountingQuantity
        }
      })
    },
    onSave() {
      if (this.disabled) return
      this.$refs["form"].validate(valid => {
        if (valid) {
          let text = this.id ? '编辑' : '新增'
          let url = this.id ? '/setting/purchase_special_policy/modify' : '/setting/purchase_special_policy/save'
          let param = {
            storeTypeId: this.form.storeTypeId,
            brandId: parseInt(this.form.brandId),
            tiresWidth: this.form.tiresWidth,
            tiresRim: this.form.tiresRim,
            tiresAspectratio: this.form.tiresAspectratio,
            figure: this.form.figure,
            isCounting: this.form.isCounting,
            maxCountingQuantity: this.form.maxCountingQuantity || 0,
            quantity: this.form.isCounting ? this.form.quantity : 0,
            beginDate: formatDate(this.form.beginDate) + ' 00:00:00',
            endDate: formatDate(this.form.endDate) + ' 23:59:59'
          }
          if (this.id) param.id = this.id
          this.status = 'loading'
          if(this.form.isCounting && !this.form.quantity) {
            this.$Notice.warning({
              title: '计算任务量不能为空'
            })
            this.status = 'pass'
            return
          }
          if(comparativeDate(this.form.beginDate, this.form.endDate)) {
            this.$Notice.warning({
              title: '开始时间不能大于结束时间'
            })
            this.status = 'pass'
            return
          }
          this.$jsonPost(url, param).then(data => {
            this.$Notice.success({
              title: `${text}成功`
            })
            this.status = 'pass'
            this.disabled = false
            this.$router.push('/policy/invalidGoods/list')
          }).catch(() => {
            this.status = 'pass'
            this.disabled = false
          })
        } else {
          this.disabled = false
        }
      })
    },
    jumpPrevPage() {
      this.$router.back()
    }
  }
}
</script>